<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/2/10
 * Time: 20:19
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\DB;

class SupplierRepaymentModel
{
    const TABLE = 'supplier_repayment';

    /**
     * @param array $data
     * @return mixed
     */
    public function getSum(array $data){
        return DB::table(self::TABLE)
            ->where(function($query)use($data){
                $query->where('supplierId',$data['supplierId']);
                if (!empty($data['startTime']) && !empty($data['endTime'])){
                    $query->where('createTime','>=',$data['startTime'])->where('createTime','<=',$data['endTime']);
                }
                if (!empty($data['projectId'])){
                    $query->where('projectId',$data['projectId']);
                }
            })
            ->sum('totalPrice');
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function lists(array $param)
    {

        $result = DB::table(self::TABLE)
            ->where(function($query)use($param){
                $query->where('supplierId',$param['supplierId']);
                if (!empty($param['month'])){
                    $startTime = (new \DateTime($param['month']))->format('Y-m-01 00:00:00');
                    $endTime = (new \DateTime($param['month']))->format('Y-m-t 23:59:59');
                    $query->whereBetween('repayTime',[$startTime,$endTime]);
                }
                if (!empty($param['projectId'])){
                    $query->where('projectId',$param['projectId']);
                }
            });

        if (!empty($param['length']) && !empty($param['page'])) {
            $limit = config('yucheng.limit');
            $start = 0;
            if (!empty($param['length'])) {
                $limit = $param['length'];
            }
            if (!empty($param['page'])) {
                $start = ($param['page'] - 1) * $limit;
            }
            $result = $result->offset($start)->limit($limit);
        }
        return $result->get()->toArray();
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function repayment(array $param)
    {
        try{
            DB::transaction(function () use($param){
                $ids = $param['orderIds'];
                unset($param['orderIds']);
                $param['createTime'] = date('Y-m-d H:i:s');
                $repaymentId = $this->insert($param);

                $supplierOrdersModel = new SupplierOrdersModel();
                foreach ($ids as $id) {
                    $info = $supplierOrdersModel->info(['id'=>$id]);
                    if (empty($info['repaymentId'])){
                        $supplierOrdersModel->update(['id'=>$id],['isPay'=>1,'repaymentId'=>$repaymentId]);
                    }else{
                         throw new \Exception('订单已经完成还款，请刷新重试');
                    }
                }
            });
            return true;
        }catch (\Exception $e){
            return $e->getMessage();
        }
    }

    /**
     * @param array $param
     * @return mixed
     */
    public function insert(array $param)
    {
        return DB::table(self::TABLE)->insertGetId($param);
    }

    public function countLists(array $param)
    {
        return DB::table(self::TABLE)
            ->where(function($query)use($param){
                $query->where('supplierId',$param['supplierId']);
                if (!empty($param['month'])){
                    $startTime = (new \DateTime($param['month']))->format('Y-m-01 00:00:00');
                    $endTime = (new \DateTime($param['month']))->format('Y-m-t 23:59:59');
                    $query->whereBetween('repayTime',[$startTime,$endTime]);
                }
                if (!empty($param['projectId'])){
                    $query->where('projectId',$param['projectId']);
                }
            })->count();

    }
}